package me.heshang.rednamedrop.Command;

import me.heshang.rednamedrop.Connection.Operation;
import me.heshang.rednamedrop.RedNamedrop;
import org.bukkit.Bukkit;
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import java.sql.SQLException;
import java.util.List;

public class Command implements CommandExecutor {
    private static Player player;
    private static Boolean send;
    private static String Message_reload;
    private static String Message_addlevelerror;
    private static String Message_removelevelerror;
    private static String Message_errorprompt;
    private static String Message_querylevelerror;
    private static List<String> Message_help;

    @Override
    public boolean onCommand(CommandSender sender, org.bukkit.command.Command command, String label, String[] args) {

        if (sender instanceof Player){
            //获取发送指令的玩家
            player = (Player) sender;
            if (args.length >= 1) {
                if (args[0].equalsIgnoreCase("reload") && sender.isOp()) {
                    RedNamedrop.getPlugin().onLoadConfig();
                    sender.sendMessage(Message_reload);
                    return false;
                }
                //查询玩家红名等级
                if (args[0].equalsIgnoreCase("query")) {
                    if (args.length == 2){
                        try {
                            int lever = new Operation().querylever(args[1]);
                            sender.sendMessage("§b|> §7"+args[1]+" level: §a"+lever);
                        } catch (SQLException e) {
                            e.printStackTrace();
                        } catch (ClassNotFoundException e) {
                            e.printStackTrace();
                        }
                        return false;
                    }else {
                        try {
                            int lever = new Operation().querylever(player.getName());
                            sender.sendMessage("§b|> §eYour level: §a"+lever);
                        } catch (SQLException e) {
                            e.printStackTrace();
                        } catch (ClassNotFoundException e) {
                            e.printStackTrace();
                        }
                        return false;
                    }
                }
                //增加玩家红名等级
                if (args[0].equalsIgnoreCase("add") && sender.isOp()) {
                    send = true;
                    if (args.length == 3){
                        try {
                            new Operation().add(args[1],Integer.parseInt(args[2]));
                        } catch (SQLException e) {
                            e.printStackTrace();
                        } catch (ClassNotFoundException e) {
                            e.printStackTrace();
                        }
                    }else {
                        player.sendMessage(Message_addlevelerror);
                    }
                    return false;
                }
                //减少玩家红名等级
                if (args[0].equalsIgnoreCase("remove") && sender.isOp() && args.length == 3) {
                    send = true;
                    try {
                        new Operation().remove(args[1],Integer.parseInt(args[2]));
                    } catch (SQLException e) {
                        e.printStackTrace();
                    } catch (ClassNotFoundException e) {
                        e.printStackTrace();
                    }
                    return false;
                }else {
                    player.sendMessage(Message_removelevelerror);
                    return false;
                }
            }
            help(player);
        }else {
            if (args.length >= 1) {
                if (args[0].equalsIgnoreCase("reload")) {
                    RedNamedrop.getPlugin().onLoadConfig();
                    sender.sendMessage(Message_reload);
                    return false;
                }
                //查询玩家红名等级
                if (args[0].equalsIgnoreCase("query")) {
                    if (args.length == 2){
                        try {
                            int lever = new Operation().querylever(args[1]);
                            sender.sendMessage("§b|> §7"+args[1]+" level: §f"+lever);
                        } catch (SQLException e) {
                            e.printStackTrace();
                        } catch (ClassNotFoundException e) {
                            e.printStackTrace();
                        }
                        return false;
                    }else {
                        Bukkit.getConsoleSender().sendMessage(Message_querylevelerror);
                        return false;
                    }
                }
                //增加玩家红名等级
                if (args[0].equalsIgnoreCase("add")) {
                    send = false;
                    if (args.length == 3){
                        try {
                            new Operation().add(args[1],Integer.parseInt(args[2]));
                        } catch (SQLException e) {
                            e.printStackTrace();
                        } catch (ClassNotFoundException e) {
                            e.printStackTrace();
                        }
                    }else {
                        Bukkit.getConsoleSender().sendMessage(Message_addlevelerror);
                    }
                    return false;
                }
                //减少玩家红名等级
                if (args[0].equalsIgnoreCase("remove")) {
                    send = false;
                    if (args.length == 3){
                        try {
                            new Operation().remove(args[1],Integer.parseInt(args[2]));
                        } catch (SQLException e) {
                            e.printStackTrace();
                        } catch (ClassNotFoundException e) {
                            e.printStackTrace();
                        }
                    }else {
                        Bukkit.getConsoleSender().sendMessage(Message_removelevelerror);
                    }
                    return false;
                }
            }else {
                senderhelp();
                return false;
            }
            System.out.println(Message_errorprompt);
        }
        return false;
    }
    public void help(Player player) {

        for (String s : Message_help){
            player.sendMessage(s);
        }

    }
    public void senderhelp(){

        for (String s : Message_help){
            Bukkit.getConsoleSender().sendMessage(s);
        }

    }
    public static Player getPlayer() {
        return player;
    }

    public static void setPlayer(Player player) {
        Command.player = player;
    }

    public static Boolean getSend() {
        return send;
    }

    public static void setSend(Boolean send) {
        Command.send = send;
    }

    public static String getMessage_reload() {
        return Message_reload;
    }

    public static void setMessage_reload(String message_reload) {
        Message_reload = message_reload;
    }

    public static String getMessage_addlevelerror() {
        return Message_addlevelerror;
    }

    public static void setMessage_addlevelerror(String message_addlevelerror) {
        Message_addlevelerror = message_addlevelerror;
    }

    public static String getMessage_removelevelerror() {
        return Message_removelevelerror;
    }

    public static void setMessage_removelevelerror(String message_removelevelerror) {
        Message_removelevelerror = message_removelevelerror;
    }

    public static String getMessage_errorprompt() {
        return Message_errorprompt;
    }

    public static void setMessage_errorprompt(String message_errorprompt) {
        Message_errorprompt = message_errorprompt;
    }

    public static List<String> getMessage_help() {
        return Message_help;
    }

    public static void setMessage_help(List<String> message_help) {
        Message_help = message_help;
    }

    public static String getMessage_querylevelerror() {
        return Message_querylevelerror;
    }

    public static void setMessage_querylevelerror(String message_querylevelerror) {
        Message_querylevelerror = message_querylevelerror;
    }
}